

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/img/avatar2.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Zhou Jie">
  <meta name="keywords" content="">
  
    <meta name="description" content="什么是TypeScript？1.是js的超集（js有的ts也有，而且ts还有js没有的东西） 2.没有的部分主要添加了类型检查，所以说名字有Type TypeScript为什么要添加类型支持？因为js经常会出现类型错误，ts就是来弥补这一缺陷的  为什么js经常会出现类型错误，ts又是如何解决的呢？ 编译语言分为静态语言（ts）和动态语言（js），静态语言是在编译时进行类型检查的，而动态语言是在运">
<meta property="og:type" content="article">
<meta property="og:title" content="Ts语法">
<meta property="og:url" content="https://jay213.gitee.io/blog/2023/08/30/Typescript%E8%AF%AD%E6%B3%95/index.html">
<meta property="og:site_name" content="Zhou Jie">
<meta property="og:description" content="什么是TypeScript？1.是js的超集（js有的ts也有，而且ts还有js没有的东西） 2.没有的部分主要添加了类型检查，所以说名字有Type TypeScript为什么要添加类型支持？因为js经常会出现类型错误，ts就是来弥补这一缺陷的  为什么js经常会出现类型错误，ts又是如何解决的呢？ 编译语言分为静态语言（ts）和动态语言（js），静态语言是在编译时进行类型检查的，而动态语言是在运">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-08-30T15:15:31.000Z">
<meta property="article:modified_time" content="2024-04-01T03:41:22.020Z">
<meta property="article:author" content="Zhou Jie">
<meta property="article:tag" content="语法">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Ts语法 - Zhou Jie</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"jay213.gitee.io","root":"/blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Indispensible</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Ts语法"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-08-30 23:15" pubdate>
          2023年8月30日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          8.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          70 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Ts语法</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="什么是TypeScript？"><a href="#什么是TypeScript？" class="headerlink" title="什么是TypeScript？"></a>什么是TypeScript？</h3><p>1.是js的超集（js有的ts也有，而且ts还有js没有的东西）</p>
<p>2.没有的部分主要添加了类型检查，所以说名字有Type</p>
<h3 id="TypeScript为什么要添加类型支持？"><a href="#TypeScript为什么要添加类型支持？" class="headerlink" title="TypeScript为什么要添加类型支持？"></a>TypeScript为什么要添加类型支持？</h3><p>因为js经常会出现类型错误，ts就是来弥补这一缺陷的</p>
<ul>
<li><p><strong>为什么js经常会出现类型错误，ts又是如何解决的呢？</strong></p>
<p>编译语言分为静态语言（ts）和动态语言（js），静态语言是在编译时进行类型检查的，而动态语言是在运行时检查类型的，由于js每次在运行时才检查类型发现错误时就已经晚了，所以会经常出现类型错误，而ts在编译时配合VSCode编译器可以在编写代码时就及时提示错误的类型并加以改正，因此运行后bug就减少了许多。</p>
</li>
</ul>
<h3 id="TypeScript相比JavaScript的优势？"><a href="#TypeScript相比JavaScript的优势？" class="headerlink" title="TypeScript相比JavaScript的优势？"></a>TypeScript相比JavaScript的优势？</h3><p>1.任何地方都有代码提示，随时随地的安全感，提升代码体验</p>
<p>2.类似地，错误也会更早地发现，减少维护成本</p>
<p>3.支持js的最新语法，让你走在技术的最前端</p>
<p>4.支持类型推断：并不用任何地方都要写类型，即享受着ts的优势，又能减少开发成本</p>
<h3 id="为什么要安装TypeScript工具包？"><a href="#为什么要安装TypeScript工具包？" class="headerlink" title="为什么要安装TypeScript工具包？"></a>为什么要安装TypeScript工具包？</h3><p>因为node.js/浏览器只认识js，不认识ts，需要TypeScript工具包将ts转化为js</p>
<p><strong>简化编译运行的包的下载：npm i -g ts-node</strong></p>
<ul>
<li>直接ts-node 文件名，不用先tsc 在node </li>
<li>在使用之前需要先 node -init</li>
<li>它是在内部转化为js然后运行js文件的，因此不会显式地生成一个js文件</li>
</ul>
<h3 id="TypeScript中的类型"><a href="#TypeScript中的类型" class="headerlink" title="TypeScript中的类型"></a>TypeScript中的类型</h3><p><strong>分为js中已有的类型和ts新增的类型</strong></p>
<ul>
<li>js中已有的类型<ul>
<li>基础类型：number、string、boolean、undefined、null、symbol（es6新增）</li>
<li>引用类型：数组、函数、对象等</li>
</ul>
</li>
<li>ts新增的类型<ul>
<li>联合类型、自定义类型（类型别名）、接口、元组、字面量类型、枚举、void、any等</li>
</ul>
</li>
</ul>
<h3 id="基础类型"><a href="#基础类型" class="headerlink" title="基础类型"></a>基础类型</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs typescript">  <span class="hljs-comment">//练习void声明，声明在ts中叫做类型注解</span><br><span class="hljs-keyword">let</span> <span class="hljs-attr">unusable1</span>:<span class="hljs-built_in">void</span>=<span class="hljs-literal">undefined</span>;<br>   <span class="hljs-keyword">let</span> <span class="hljs-attr">unusable2</span>:<span class="hljs-built_in">void</span>=<span class="hljs-literal">null</span>;<br>   <span class="hljs-keyword">function</span> <span class="hljs-title function_">warnUser</span>(<span class="hljs-params"></span>):<span class="hljs-built_in">void</span>&#123;<br>      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;This is a warning message!&quot;</span>)<br>   &#125;<br><span class="hljs-comment">//undefined、null</span><br><span class="hljs-keyword">let</span> <span class="hljs-attr">u</span>:<span class="hljs-literal">undefined</span>=<span class="hljs-literal">undefined</span>;<br><span class="hljs-keyword">let</span> <span class="hljs-attr">n</span>:<span class="hljs-literal">null</span>=<span class="hljs-literal">null</span>;<br><span class="hljs-keyword">let</span> <span class="hljs-attr">num</span>:<span class="hljs-built_in">number</span>=<span class="hljs-literal">null</span>;<span class="hljs-comment">//null和undfined是所有类型的子类，可看作初始化吧</span><br><br><span class="hljs-comment">//never表示不存在的值的类型，比如函数中死循环或者抛出异常的返回值类型</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">infiniteLoop</span>(<span class="hljs-params"></span>)&#123;<br> <span class="hljs-keyword">while</span>(<span class="hljs-literal">true</span>)&#123;<br>	 <br> &#125;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">error</span>(<span class="hljs-params">message:<span class="hljs-built_in">String</span></span>)&#123;<br> <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(message);<br>&#125;<br><br><span class="hljs-comment">//object类型是除了number、string、boolean、undefined或null之外的类型</span><br><br><span class="hljs-comment">//类型断言：在编译时告诉编译器&quot;我来手动指定类型，并且请你放心&quot;</span><br><span class="hljs-comment">//方式一</span><br><span class="hljs-keyword">let</span> <span class="hljs-attr">someValue</span>:<span class="hljs-built_in">any</span>=<span class="hljs-string">&quot;a value&quot;</span>;<br><span class="hljs-keyword">let</span> <span class="hljs-attr">strLength</span>:<span class="hljs-built_in">number</span>=(&lt;<span class="hljs-built_in">string</span>&gt;someValue).<span class="hljs-property">length</span>;<br><span class="hljs-comment">//方式二</span><br><span class="hljs-keyword">let</span> <span class="hljs-attr">strLength</span>:<span class="hljs-built_in">number</span>=(someValue <span class="hljs-keyword">as</span> <span class="hljs-built_in">string</span>).<span class="hljs-property">length</span>;<br></code></pre></td></tr></table></figure>

<h3 id="变量的声明"><a href="#变量的声明" class="headerlink" title="变量的声明"></a>变量的声明</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">//var</span><br><span class="hljs-comment">/*作用域</span><br><span class="hljs-comment">1.在函数内任何地方声明作用域都为整个函数内</span><br><span class="hljs-comment">2.在函数内直接声明，不加var则表示全局变量，否则是局部变量</span><br><span class="hljs-comment">3.在函数外不管加不加var则都是全局变量</span><br><span class="hljs-comment">*/</span><br>   <span class="hljs-comment">/*setTimeout异步处理机制：</span><br><span class="hljs-comment">  主线程里面的执行栈存放同步任务，还有个任务队列，只要有异步任务就会增加一个事件存放在任务队列</span><br><span class="hljs-comment">  系统先按顺序执行完所有执行栈中的同步任务，然后就会读取任务队列执行其中的事件解除对应异步任务的等待状态并将异步任务放到执行栈中执行</span><br><span class="hljs-comment">  直到任务队列中所有任务被执行完</span><br><span class="hljs-comment">*/</span><br>   <span class="hljs-comment">//let</span><br><span class="hljs-comment">/*</span><br><span class="hljs-comment">作用域：局部作用域</span><br><span class="hljs-comment">其中捕获的异常也是局部的，例如这里的e在括号外是无法取到的</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">try</span>&#123;<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;Oh No!&quot;</span>)<br>&#125;<span class="hljs-keyword">catch</span>(e)&#123;<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;Oh Well!&quot;</span>)<br>&#125;<br>   <span class="hljs-comment">/*</span><br><span class="hljs-comment">正常来说 块级作用域变量在声明之前是无法使用的，但有个特例：函数的声明，但是调用却不行</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) &#123;<br>     <span class="hljs-comment">//okay to capture &#x27;a&#x27;</span><br>     <span class="hljs-keyword">return</span> a;<br>   &#125;<br><span class="hljs-title function_">foo</span>();<span class="hljs-comment">//报错</span><br>   <span class="hljs-keyword">let</span> a=<span class="hljs-number">1</span>;<br>   <span class="hljs-comment">/*能用const就用const，是为了安全*/</span><br>   <span class="hljs-comment">//解构：解析一个数据的结构，是通过给这个数据每个值(不一定每一个，一一对应就行)定义一个变量来实现的，由于这个数据的结构由自定义的变量组成，因为它的结构整体上来看更清晰，更方便使用</span><br>   <span class="hljs-comment">//解构其实就是c++中的别名，既能访问，又能修改值（两边都变）</span><br>  <span class="hljs-comment">//1.解构数组</span><br>  <span class="hljs-keyword">let</span> input=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>];<br>  <span class="hljs-keyword">let</span> [first,second]=input;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(first)<span class="hljs-comment">//1</span><br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(second)<span class="hljs-comment">//2</span><br>  <span class="hljs-comment">//交换变量很方便</span><br>  [first,second]=[second,first]<br>  <span class="hljs-comment">//作为函数参数</span><br>  <span class="hljs-keyword">function</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">[first,second]:[<span class="hljs-built_in">number</span>,<span class="hljs-built_in">number</span>]</span>)&#123;<br>       <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(first)<br>       <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(second)<br>  &#125;<br>  <span class="hljs-title function_">f</span>(input);<br>  <span class="hljs-comment">//可使…创建剩余变量</span><br>  <span class="hljs-keyword">let</span> [first,…rest]=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(first)<span class="hljs-comment">//1</span><br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(rest)<span class="hljs-comment">//2,3,4</span><br>  <span class="hljs-comment">//只定义一个变量时默认是第一个，没有定义全时，一一对应即可</span><br>  <span class="hljs-keyword">let</span> [first]=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(first)<span class="hljs-comment">//1</span><br>  <span class="hljs-keyword">let</span> [first,,third]=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(third)<span class="hljs-comment">//3</span><br>  <span class="hljs-comment">//2.解构对象</span><br>  <span class="hljs-keyword">let</span> o=&#123;<br>      <span class="hljs-attr">a</span>:<span class="hljs-string">&quot;a&quot;</span>,<br>      <span class="hljs-attr">b</span>:<span class="hljs-number">123</span>,<br>      <span class="hljs-attr">c</span>:<span class="hljs-string">&quot;cc&quot;</span><br>  &#125;<br>  <span class="hljs-keyword">let</span> &#123;a,b&#125;=o;<span class="hljs-comment">//一一对应</span><br>  <span class="hljs-comment">//用…声明剩余变量</span><br>  <span class="hljs-keyword">let</span> &#123;a,…passthrough&#125;=o;<br>  <span class="hljs-keyword">let</span> total=passthrough.<span class="hljs-property">b</span>+passthrough.<span class="hljs-property">c</span>.<span class="hljs-property">length</span><br>  <span class="hljs-comment">//属性重命名：在解构时可以对自定义的变量重新命名</span><br>  <span class="hljs-keyword">let</span> &#123;<span class="hljs-attr">a</span>:newName1,<span class="hljs-attr">b</span>:newName2&#125;=o;<span class="hljs-comment">//这里的冒号并不是指声明数据类型</span><br>  <span class="hljs-comment">//给解构变量声明数据类型:需要在整体后面加冒号，并且重新用括号一一制定</span><br>  <span class="hljs-keyword">let</span> &#123;a,b&#125;:&#123;<span class="hljs-attr">a</span>:<span class="hljs-built_in">string</span>,<span class="hljs-attr">b</span>:<span class="hljs-built_in">number</span>&#125;=o;<br>  <span class="hljs-comment">//4.默认值，目的是针对undfined的值有个缺省值</span><br>  <span class="hljs-keyword">function</span> <span class="hljs-title function_">fun</span>(<span class="hljs-params"><span class="hljs-built_in">object</span>:&#123;a:<span class="hljs-built_in">string</span>,b?:<span class="hljs-built_in">number</span>&#125;</span>)&#123;<br>      <span class="hljs-keyword">let</span> &#123;a,b=<span class="hljs-number">100</span>&#125;=<span class="hljs-built_in">object</span>;<br>  &#125;<span class="hljs-comment">//b原来有个?表示undfined，现在object的b就有默认值不再是undfined了</span><br>  <span class="hljs-comment">//函数声明</span><br>  <span class="hljs-keyword">function</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">&#123;a,b=<span class="hljs-number">0</span>&#125;=&#123;a=<span class="hljs-string">&quot;&quot;</span>&#125;</span>):<span class="hljs-built_in">void</span>&#123;<br>      ...<br>  &#125;<br>  <span class="hljs-comment">//从这里可以看出解构不仅仅是“别名”，若原来没有的还可以添加上去，比如这里的b而且还有缺省值</span><br>  <span class="hljs-title function_">f</span>(&#123;<span class="hljs-attr">a</span>:<span class="hljs-string">&quot;123&quot;</span>&#125;)--a=<span class="hljs-string">&quot;123&quot;</span>,b=<span class="hljs-number">0</span><br>  <span class="hljs-title function_">f</span>()----a=<span class="hljs-string">&quot;&quot;</span>,b=<span class="hljs-number">0</span><br>  <span class="hljs-title function_">f</span>(&#123;&#125;)----error, <span class="hljs-string">&#x27;a&#x27;</span> is required <span class="hljs-keyword">if</span> you supply an argument<br>  <span class="hljs-comment">//以上例子意思就是说不提供参数可以，因为都有缺省值可是已经确定提供参数了，那就说明不要缺省值，而你有没有替代缺省值的值，那肯定报错啊</span><br>  <span class="hljs-comment">//3.展开表达式：和解构相反，解构是将数据拆开来使用，而展开是将各个合并起来使用，展开指的是合并后的状态</span><br>  <span class="hljs-comment">//3.1 数组展开</span><br>  <span class="hljs-keyword">let</span> first = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];<br>  <span class="hljs-keyword">let</span> second = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>];<br>  <span class="hljs-keyword">let</span> bothPlus = [<span class="hljs-number">0</span>,first,second,<span class="hljs-number">5</span>];<span class="hljs-comment">//0,1,2,3,4,5,浅拷贝，原来first second不会改变</span><br>  <span class="hljs-comment">//3.2 对象展开</span><br>  <span class="hljs-keyword">let</span> defaults = &#123; <span class="hljs-attr">food</span>: <span class="hljs-string">&quot;spicy&quot;</span>, <span class="hljs-attr">price</span>: <span class="hljs-string">&quot;$$&quot;</span>, <span class="hljs-attr">ambiance</span>: <span class="hljs-string">&quot;noisy&quot;</span> &#125;;<br>  <span class="hljs-keyword">let</span> search = &#123; ...defaults, <span class="hljs-attr">food</span>: <span class="hljs-string">&quot;rich&quot;</span> &#125;;<br>  <span class="hljs-comment">/*结果为food: &quot;spicy&quot;, price: &quot;$$&quot;, ambiance: &quot;noisy&quot;，food: &quot;rich&quot;</span><br><span class="hljs-comment">    但是 如果声明为 let search = &#123; food: &quot;rich&quot;, ...defaults &#125;;</span><br><span class="hljs-comment">    添加进来的若有重名的会替换，即只有一个后面的food</span><br><span class="hljs-comment">  */</span><br>  <span class="hljs-comment">//对象展开还有个限制：展开对象实例时只能访问属性无法访问方法</span><br>  <span class="hljs-keyword">class</span> <span class="hljs-title class_">C</span> &#123;<br>    p = <span class="hljs-number">12</span>;<br>    <span class="hljs-title function_">m</span>(<span class="hljs-params"></span>) &#123;<br>   &#125;<br>  &#125;<br> <span class="hljs-keyword">let</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title function_">C</span>();<br> <span class="hljs-keyword">let</span> clone = &#123; ...c &#125;;<br> clone.<span class="hljs-property">p</span>; <span class="hljs-comment">// ok</span><br> clone.<span class="hljs-title function_">m</span>(); <span class="hljs-comment">// error!</span><br></code></pre></td></tr></table></figure>

<h3 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">//1.接口初探：接口只是代表着一种对象的规范</span><br><span class="hljs-comment">//一般用于类型检查，即传过来的对象是是否符合规范，检查规则是是否包含接口中的属性，顺序可以不一样</span><br><span class="hljs-keyword">interface</span> labeledValue&#123;<br>    <span class="hljs-attr">label</span>:<span class="hljs-built_in">string</span>;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">printLabel</span>(<span class="hljs-params">labeledObj:labeledValue</span>)&#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(labeledObj.<span class="hljs-property">label</span>)<br>&#125;<br><span class="hljs-keyword">let</span> myObj=&#123;<span class="hljs-attr">size</span>:<span class="hljs-number">10</span>,<span class="hljs-attr">label</span>:<span class="hljs-string">&quot;Size 10 Object&quot;</span>&#125;<br><span class="hljs-title function_">printLabel</span>(myObj)<br>------------------------------------------------------------------------------------<br><span class="hljs-comment">//2.可选属性：接口中属性的?表示可选，和没有可选属性的接口的区别是：有可选属性的接口的属性个数通常大于等于传入对象属性的个数，而没有可选属性的接口的属性个数通常小于等于传入对象属性的个数</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">SquareConfig</span> &#123;<br>  color?: <span class="hljs-built_in">string</span>;<br>  width?: <span class="hljs-built_in">number</span>;<br>&#125;<br><span class="hljs-comment">//注意：对象在声明时属性之间是分号，实例化之后是逗号</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createSquare</span>(<span class="hljs-params">config: SquareConfig</span>): &#123;<span class="hljs-attr">color</span>: <span class="hljs-built_in">string</span>; <span class="hljs-attr">area</span>:<span class="hljs-built_in">number</span>&#125; &#123;<br>  <span class="hljs-keyword">let</span> newSquare = &#123;<span class="hljs-attr">color</span>: <span class="hljs-string">&quot;white&quot;</span>, <span class="hljs-attr">area</span>: <span class="hljs-number">100</span>&#125;;<br>  <span class="hljs-keyword">if</span> (config.<span class="hljs-property">color</span>) &#123;<br>    newSquare.<span class="hljs-property">color</span> = config.<span class="hljs-property">color</span>;<br>  &#125;<br>  <span class="hljs-keyword">if</span> (config.<span class="hljs-property">width</span>) &#123;<br>    newSquare.<span class="hljs-property">area</span> = config.<span class="hljs-property">width</span> * config.<span class="hljs-property">width</span>;<br>  &#125;<br>  <span class="hljs-keyword">return</span> newSquare;<br>&#125;<br><span class="hljs-keyword">let</span> mySquare = <span class="hljs-title function_">createSquare</span>(&#123;<span class="hljs-attr">color</span>: <span class="hljs-string">&quot;black&quot;</span>&#125;);<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(mySquare)<br>------------------------------------------------------------------------------------<br><span class="hljs-comment">//3.只读属性：</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Point</span> &#123;<br>    <span class="hljs-keyword">readonly</span> <span class="hljs-attr">x</span>: <span class="hljs-built_in">number</span>;<br>    <span class="hljs-keyword">readonly</span> <span class="hljs-attr">y</span>: <span class="hljs-built_in">number</span>;<br>&#125;<br><span class="hljs-keyword">let</span> <span class="hljs-attr">p1</span>: <span class="hljs-title class_">Point</span> = &#123; <span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span> &#125;;<br>p1.<span class="hljs-property">x</span> = <span class="hljs-number">5</span>; <span class="hljs-comment">// error!</span><br><span class="hljs-comment">//readOnly是针对属性的，而const是针对变量的</span><br><span class="hljs-comment">//4.额外的属性检查</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">SquareConfig</span> &#123;<br>    color?: <span class="hljs-built_in">string</span>;<br>    width?: <span class="hljs-built_in">number</span>;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createSquare</span>(<span class="hljs-params">config: SquareConfig</span>): &#123; <span class="hljs-attr">color</span>: <span class="hljs-built_in">string</span>; <span class="hljs-attr">area</span>: <span class="hljs-built_in">number</span> &#125; &#123;<br>    <span class="hljs-comment">// ...</span><br>&#125;<br><span class="hljs-keyword">let</span> mySquare = <span class="hljs-title function_">createSquare</span>(&#123; <span class="hljs-attr">colour</span>: <span class="hljs-string">&quot;red&quot;</span>, <span class="hljs-attr">width</span>: <span class="hljs-number">100</span> &#125;);<span class="hljs-comment">//故意把color拼错</span><br><span class="hljs-comment">//此时ts会进行额外的属性检查：既然传入了一个两个属性的对象，那么ts就会注意检查是否和定义的两个属性的数据类型一样，如果只有width属性是可以的，因为color是可选的。简而言之，就是可选属性是指少一些属性可以，但是如果不少那么类型必须一样否则报错</span><br><span class="hljs-comment">//4.1 也可以绕开属性检查，方法一-----类型断言（强制类型转换）</span><br><span class="hljs-keyword">let</span> mySquare = <span class="hljs-title function_">createSquare</span>(&#123; <span class="hljs-attr">opacity</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">width</span>: <span class="hljs-number">100</span> &#125; <span class="hljs-keyword">as</span> <span class="hljs-title class_">SquareConfig</span>);<br><span class="hljs-comment">//转换了之后参数就变为只有width的SquareConfig，可选属性就用上了</span><br><span class="hljs-comment">//4.3 方法二-----加一个索引签名：就是就是键值对（对象的属性本来就是键值对的形式定义的）</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">SquareConfig</span>&#123;<br>    color?: <span class="hljs-built_in">string</span>;<br>    width?: <span class="hljs-built_in">number</span>;<br>    [<span class="hljs-attr">propName</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-built_in">any</span>;<span class="hljs-comment">//键为字符串，值为任意类型</span><br>&#125;<br><span class="hljs-comment">//4.4 方法三----对象赋值给另外一个变量（就好比原来是匿名对象后来有名字了）</span><br><span class="hljs-keyword">let</span> squareOptions=&#123; <span class="hljs-attr">colour</span>: <span class="hljs-string">&quot;red&quot;</span>, <span class="hljs-attr">width</span>: <span class="hljs-number">100</span> &#125;;<br><span class="hljs-keyword">let</span> mySquare = <span class="hljs-title function_">createSquare</span>(squareOptions);<br>------------------------------------------------------------------------------------<br><span class="hljs-comment">//5.使用接口也可以定义函数</span><br><span class="hljs-keyword">interface</span> searchFunc&#123;<br>    (<span class="hljs-attr">source</span>:<span class="hljs-built_in">string</span>,<span class="hljs-attr">subString</span>:<span class="hljs-built_in">string</span>):<span class="hljs-built_in">boolean</span>;<br>&#125; <br><span class="hljs-keyword">let</span> <span class="hljs-attr">mySearch</span>:searchFunc;<br>mySearch=<span class="hljs-keyword">function</span>(<span class="hljs-params">src:<span class="hljs-built_in">string</span>,sub:<span class="hljs-built_in">string</span></span>):<span class="hljs-built_in">boolean</span>&#123;<span class="hljs-comment">//名字可以不一样，重要的是检查类型</span><br>    <span class="hljs-keyword">let</span> result=src.<span class="hljs-title function_">search</span>(subString);<br>    retrurn result&gt;-<span class="hljs-number">1</span>;<br>&#125;<span class="hljs-comment">//mySearch(参数一，参数二)调用</span><br><span class="hljs-comment">//5.1 如果如果参数类型或返回值类型没写，ts还可以推断，在调用时根据传入的参数来判断然后再检查</span><br><span class="hljs-keyword">let</span> <span class="hljs-attr">mySearch</span>: <span class="hljs-title class_">SearchFunc</span>;<br>mySearch = <span class="hljs-keyword">function</span>(<span class="hljs-params">src, sub</span>) &#123;<br>    <span class="hljs-keyword">let</span> result = src.<span class="hljs-title function_">search</span>(sub);<br>    <span class="hljs-keyword">return</span> result &gt; -<span class="hljs-number">1</span>;<br>&#125;<br>------------------------------------------------------------------------------------<br><span class="hljs-comment">//6. 接口还可以定义可索引的类型（索引签名）</span><br><span class="hljs-keyword">interface</span> stringArray&#123;<br>    [<span class="hljs-attr">index</span>:<span class="hljs-built_in">number</span>]:<span class="hljs-built_in">string</span>;<br>&#125;<br><span class="hljs-keyword">let</span> <span class="hljs-attr">myArray</span>:stringArray;<br>myArray=[<span class="hljs-string">&quot;1&quot;</span>,<span class="hljs-string">&quot;2&quot;</span>];<br><span class="hljs-keyword">let</span> myStr=myArray[<span class="hljs-number">0</span>];<br><span class="hljs-comment">//6.1 索引签名分为数值型和字符串型，接口可以同时定义这两个，（好像）但是名字要一样，于是最后只会用一个</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Animal</span>&#123;<br>    <span class="hljs-attr">name</span>:<span class="hljs-built_in">string</span>;<br>&#125;<br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Dog</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">Animal</span>&#123;<br>    <span class="hljs-attr">breed</span>:<span class="hljs-built_in">string</span>;<br>&#125;<br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">NotOk</span>&#123;<br>    <span class="hljs-comment">//错误，数值型索引签名的返回值必须为字符串型索引签名的返回值的子类型，因为使用number来索引时，js会默认将数值型转换为字符串型然后再去索引对象，也就是说1最后会被当做&quot;1&quot;</span><br>    [<span class="hljs-attr">x</span>:<span class="hljs-built_in">number</span>]:<span class="hljs-title class_">Animal</span>;        [<span class="hljs-attr">x</span>:<span class="hljs-built_in">number</span>]:<span class="hljs-title class_">Dog</span>;---ok<br>    [<span class="hljs-attr">x</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-title class_">Dog</span>;           [<span class="hljs-attr">x</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-title class_">Animal</span>;----ok<br>    <span class="hljs-comment">//其实就是如下所示：上面的number变为string以后，如果传入的是100，那么就会变成&quot;100&quot;，第一个满足，则使用第一个类型，如果本来传入的就是&quot;100&quot;,那么就用第二个（此时第一个还是number，只有传入是数字型第一个才为字符型），后面的子类是为了 当都是string型时优先用子类的（多态）</span><br>    [<span class="hljs-attr">x</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-title class_">Dog</span>; <br>    [<span class="hljs-attr">x</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-title class_">Animal</span>;   <br>&#125;<br><span class="hljs-comment">//6.2 其中字符串索引声明还指明了接口属性的返回值，如果有属性返回值与字符串索引返回值不一致则会报错，可以把接口中的字符串索引当作是其他属性的规范,因为属性名默认都是string类型的</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">NumberDictionary</span>&#123;<br>    <span class="hljs-keyword">readonly</span> [<span class="hljs-attr">index</span>:<span class="hljs-built_in">string</span>]:<span class="hljs-built_in">number</span>;<span class="hljs-comment">//readonly加上可以不能被修改</span><br>    <span class="hljs-attr">length</span>:<span class="hljs-built_in">number</span>;<br>    <span class="hljs-attr">name</span>:<span class="hljs-built_in">string</span>;<span class="hljs-comment">//错误，应该为number</span><br>&#125;<br>------------------------------------------------------------------------------------<br><span class="hljs-comment">//7.和java一样可以用类实现接口</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">ClockInterface</span> &#123;<br>    <span class="hljs-attr">currentTime</span>: <span class="hljs-title class_">Date</span>;<br>    <span class="hljs-title function_">setTime</span>(<span class="hljs-attr">d</span>: <span class="hljs-title class_">Date</span>);<br>&#125;<br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Clock</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ClockInterface</span> &#123;<br>    <span class="hljs-attr">currentTime</span>: <span class="hljs-title class_">Date</span>;<br>    <span class="hljs-title function_">setTime</span>(<span class="hljs-params">d: <span class="hljs-built_in">Date</span></span>) &#123;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">currentTime</span> = d;<br>    &#125;<br>    <span class="hljs-title function_">constructor</span>(<span class="hljs-params">h: <span class="hljs-built_in">number</span>, m: <span class="hljs-built_in">number</span></span>) &#123; &#125;<br>&#125;<br>------------------------------------------------------------------------------------<br><br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/Ts/" class="category-chain-item">Ts</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/%E8%AF%AD%E6%B3%95/">#语法</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Ts语法</div>
      <div>https://jay213.gitee.io/blog/2023/08/30/Typescript语法/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Zhou Jie</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年8月30日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2023/08/31/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="git常用命令">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">git常用命令</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/08/30/Angular%E6%9B%B4%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84%E6%B3%A8%E6%84%8F%E7%82%B9/" title="Angular更新版本的注意点">
                        <span class="hidden-mobile">Angular更新版本的注意点</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div class="disqus" style="width:100%">
    <div id="disqus_thread"></div>
    
      <script type="text/javascript">
        var disqus_config = function() {
          this.page.url = 'https://jay213.gitee.io/blog/2023/08/30/Typescript%E8%AF%AD%E6%B3%95/';
          this.page.identifier = '/blog/2023/08/30/Typescript%E8%AF%AD%E6%B3%95/';
        };
        Fluid.utils.loadComments('#disqus_thread', function() {
          var d = document, s = d.createElement('script');
          s.src = '//' + 'fluid' + '.disqus.com/embed.js';
          s.setAttribute('data-timestamp', new Date());
          (d.head || d.body).appendChild(s);
        });
      </script>
    
    <noscript>Please enable JavaScript to view the comments</noscript>
  </div>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
